<?php

/**
	Theme Admin Library

	The contents of this file are subject to the terms of the GNU General
	Public License Version 2.0. You may not use this file except in
	compliance with the license. Any of the license terms and conditions
	can be waived if you get permission from the copyright holder.

	Copyright (c) 2011 Bandit Media
	Jermaine Marée

		@package BanditAdmin
		@version 1.1
**/

//! Theme specific methods
class BanditAdmin extends FeatherTheme {

	protected static
		//! Plugin Hook
		$hook,
		//! Tabs
		$tabs;
	
	public static
		//! Module Tabs
		$module_tabs;

	/**
		Admin methods and actions
			@public
	**/
	static function boot() {
		// Admin init
		add_action('admin_init',__CLASS__.'::init');
		// Admin menu
		add_action('admin_menu',__CLASS__.'::menu');
		// Delete attachment action
		if(current_user_can('delete_posts')) {
			add_action('delete_attachment',__CLASS__.'::delete_dynamic_images',10);
		}
	}

	/**
		Admin init
			@public
	**/
	static function init() {
		// Set Hook
		self::$hook = get_plugin_page_hook('bandit','themes.php');
		// Set Tabs
		self::$tabs = array(
			'theme'		=> 'Creatic',
			'portfolio'	=> __('Portfolio','creatic'),
			'general'	=> __('General','creatic'),
			'language'	=> __('Language','creatic')
		);
		// Set Module Tabs
		self::$module_tabs = array(
			'sliders'	=> 'Sliders',
			'social'	=> 'Social'
		);
		// Posts Javascript
		add_action('admin_print_scripts-post-new.php',__CLASS__.'::posts_javascript');
		add_action('admin_print_scripts-post.php',__CLASS__.'::posts_javascript');
		// Register theme settings
		register_setting('bandit-settings',self::$option_name,
			'BanditValidate::init');
		// Register language settings
		register_setting('bandit-language','bandit_language',
			'BanditValidate::language');
		// Load validation library
		require(FEATHER_THEME_PATH.'lib/bandit-validate.php');
		// Plugin-specific actions
		add_action('load-'.self::$hook,__CLASS__.'::bandit_init');
		// Plugin styles
		add_action('admin_print_styles-'.self::$hook,__CLASS__.'::styles');
		// Plugin scripts
		add_action('admin_print_scripts-'.self::$hook,__CLASS__.'::scripts');
	}

	/**
		Delete dynamic images
			@public
	**/
	static function delete_dynamic_images($post_id) {
		// Get upload dir and path
		$upload_dir = wp_upload_dir();
		$upload_path = $upload_dir['basedir'];
		global $wpdb;
		$results=$wpdb->get_results("SELECT * FROM bandit_images WHERE post_id = '$post_id' ", ARRAY_A);
		if($results) {
			$images = array();
			// Set full image paths
			foreach($results as $result)
				$images[] = $upload_path.$result['image_file'];
			// Delete images
			foreach($images as $image)
				@unlink($image);
			// Delete DB entries
			$wpdb->query("DELETE FROM bandit_images WHERE post_id = '$post_id' ");
		}
	}

	/**
		Post formats javascript
			@public
	**/
	static function posts_javascript() {
		wp_enqueue_script('theme-admin-postformats',FEATHER_THEME_URL.
			'assets/js/theme-admin-posts.js',array('jquery'),20110921);
	}

	/**
		Admin menu
			@public
	**/
	static function menu() {
		// Theme Options
		add_theme_page('Theme Options','Theme Options','edit_theme_options',
			'bandit',__CLASS__.'::options_page');
		// Theme Modules
		add_theme_page('Theme Modules','Theme Modules','edit_theme_options',
			'banditmod',__CLASS__.'::modules_page');
	}

	/**
		Options page
			@public
	**/
	static function options_page() {
		$tabs = self::$tabs;
		$current_tab = FeatherAdmin::get_current_tab($tabs);
		require(FEATHER_THEME_PATH.'tmpl/theme-options-page.php');
	}

	/**
		Modules page
			@public
	**/
	static function modules_page() {
		$tabs = self::$module_tabs;
		$current_tab = FeatherAdmin::get_current_tab($tabs);
		require(FEATHER_THEME_PATH.'tmpl/theme-modules-page.php');
	}

	/**
		Theme settings page init
			@public
	**/
	static function bandit_init() {
		// Load settings library
		require(FEATHER_PATH.'lib/feather-settings.php');
		$current_tab = FeatherAdmin::get_current_tab(self::$tabs);
		// Get settings
		if($current_tab!='language') {
			$settings = FeatherConfig::load('settings','setting');
			$option_name = self::$option_name;
		} else {
			$settings = FeatherConfig::load('language','lang');
			$option_name = 'bandit_language';
		}
		// Initialize settings library
		FeatherSettings::init($option_name,$settings);
	}

	/**
		Scripts
			@public
	**/
	static function scripts() {
		// Javascript URL
		$url = FEATHER_THEME_URL.'assets/js/';
		// Javascript Path
		$path = FEATHER_THEME_PATH.'assets/js/';
		// Register colorpicker.js
		$colorpicker = FEATHER_URL.'assets/js/colorpicker.js';
		wp_register_script('bandit-color-js',$colorpicker,array('jquery'));
		// theme-admin.js Dependencies
		$js_dep = array('media-upload','thickbox','jquery','bandit-color-js');
		// theme-admin.js Version
		$js_ver = filemtime($path.'theme-admin.js');
		// Enqueue feather.js script
		wp_enqueue_script('bandit-js',$url.'theme-admin.js',$js_dep,$js_ver);
	}

	/**
		Styles
			@public
	**/
	static function styles() {
		// CSS URL
		$url = FEATHER_THEME_URL.'assets/css/';
		// CSS Path
		$path = FEATHER_THEME_PATH.'assets/css/';
		// Register colorpicker.css
		$colorpicker = FEATHER_URL.'assets/css/colorpicker.css';
		wp_register_style('bandit-color-css',$colorpicker,FALSE);
		// theme-admin.css Dependencies
		$css_dep = array('thickbox','bandit-color-css');
		// theme-admin.css Version
		$css_ver = filemtime($path.'theme-admin.css');
		// Enqueue style
		wp_enqueue_style('bandit-css',$url.'theme-admin.css',$css_dep,$css_ver);
	}

}
